﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:AreaType
    /// </summary>
    public partial class AreaType
    {
        public AreaType()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("AreaTypeID", "AreaType");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int AreaTypeID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from AreaType");
            strSql.Append(" where AreaTypeID=@AreaTypeID");
            var parameters = new SqlParameter[] { new SqlParameter("@AreaTypeID", SqlDbType.Int, 4)
            };
            parameters[0].Value = AreaTypeID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.AreaType model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into AreaType(");
            strSql.Append("Rtypeid,Parid,Leveal,Rsonnum,RsonCount,RuserCode,Rfullname,Rname,RComment,RNamePY)");
            strSql.Append(" values (");
            strSql.Append("@Rtypeid,@Parid,@Leveal,@Rsonnum,@RsonCount,@RuserCode,@Rfullname,@Rname,@RComment,@RNamePY)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@Rtypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Parid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@Rsonnum", SqlDbType.Int, 4),
                    new SqlParameter("@RsonCount", SqlDbType.Int, 4),
                    new SqlParameter("@RuserCode", SqlDbType.VarChar, 66),
                    new SqlParameter("@Rfullname", SqlDbType.VarChar, 100),
                    new SqlParameter("@Rname", SqlDbType.VarChar, 50),
                    new SqlParameter("@RComment", SqlDbType.VarChar, 256),
                    new SqlParameter("@RNamePY", SqlDbType.VarChar, 100) };
            parameters[0].Value = model.Rtypeid;
            parameters[1].Value = model.Parid;
            parameters[2].Value = model.Leveal;
            parameters[3].Value = model.Rsonnum;
            parameters[4].Value = model.RsonCount;
            parameters[5].Value = model.RuserCode;
            parameters[6].Value = model.Rfullname;
            parameters[7].Value = model.Rname;
            parameters[8].Value = model.RComment;
            parameters[9].Value = model.RNamePY;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.AreaType model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update AreaType set ");
            strSql.Append("Rtypeid=@Rtypeid,");
            strSql.Append("Parid=@Parid,");
            strSql.Append("Leveal=@Leveal,");
            strSql.Append("Rsonnum=@Rsonnum,");
            strSql.Append("RsonCount=@RsonCount,");
            strSql.Append("RuserCode=@RuserCode,");
            strSql.Append("Rfullname=@Rfullname,");
            strSql.Append("Rname=@Rname,");
            strSql.Append("RComment=@RComment,");
            strSql.Append("RNamePY=@RNamePY");
            strSql.Append(" where AreaTypeID=@AreaTypeID");
            var parameters = new SqlParameter[] { new SqlParameter("@Rtypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Parid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@Rsonnum", SqlDbType.Int, 4),
                    new SqlParameter("@RsonCount", SqlDbType.Int, 4),
                    new SqlParameter("@RuserCode", SqlDbType.VarChar, 66),
                    new SqlParameter("@Rfullname", SqlDbType.VarChar, 100),
                    new SqlParameter("@Rname", SqlDbType.VarChar, 50),
                    new SqlParameter("@RComment", SqlDbType.VarChar, 256),
                    new SqlParameter("@RNamePY", SqlDbType.VarChar, 100),
                    new SqlParameter("@AreaTypeID", SqlDbType.Int, 4) };
            parameters[0].Value = model.Rtypeid;
            parameters[1].Value = model.Parid;
            parameters[2].Value = model.Leveal;
            parameters[3].Value = model.Rsonnum;
            parameters[4].Value = model.RsonCount;
            parameters[5].Value = model.RuserCode;
            parameters[6].Value = model.Rfullname;
            parameters[7].Value = model.Rname;
            parameters[8].Value = model.RComment;
            parameters[9].Value = model.RNamePY;
            parameters[10].Value = model.AreaTypeID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int AreaTypeID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from AreaType ");
            strSql.Append(" where AreaTypeID=@AreaTypeID");
            var parameters = new SqlParameter[] { new SqlParameter("@AreaTypeID", SqlDbType.Int, 4)
            };
            parameters[0].Value = AreaTypeID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string AreaTypeIDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from AreaType ");
            strSql.Append(" where AreaTypeID in (" + AreaTypeIDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.AreaType GetModel(int AreaTypeID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 AreaTypeID,Rtypeid,Parid,Leveal,Rsonnum,RsonCount,RuserCode,Rfullname,Rname,RComment,RNamePY from AreaType ");
            strSql.Append(" where AreaTypeID=@AreaTypeID");
            var parameters = new SqlParameter[] { new SqlParameter("@AreaTypeID", SqlDbType.Int, 4)
            };
            parameters[0].Value = AreaTypeID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.AreaType DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.AreaType();
            if (row != null)
            {
                if (row["AreaTypeID"] != null && row["AreaTypeID"].ToString() != string.Empty)
                {
                    model.AreaTypeID = int.Parse(row["AreaTypeID"].ToString());
                }
                if (row["Rtypeid"] != null)
                {
                    model.Rtypeid = row["Rtypeid"].ToString();
                }
                if (row["Parid"] != null)
                {
                    model.Parid = row["Parid"].ToString();
                }
                if (row["Leveal"] != null && row["Leveal"].ToString() != string.Empty)
                {
                    model.Leveal = int.Parse(row["Leveal"].ToString());
                }
                if (row["Rsonnum"] != null && row["Rsonnum"].ToString() != string.Empty)
                {
                    model.Rsonnum = int.Parse(row["Rsonnum"].ToString());
                }
                if (row["RsonCount"] != null && row["RsonCount"].ToString() != string.Empty)
                {
                    model.RsonCount = int.Parse(row["RsonCount"].ToString());
                }
                if (row["RuserCode"] != null)
                {
                    model.RuserCode = row["RuserCode"].ToString();
                }
                if (row["Rfullname"] != null)
                {
                    model.Rfullname = row["Rfullname"].ToString();
                }
                if (row["Rname"] != null)
                {
                    model.Rname = row["Rname"].ToString();
                }
                if (row["RComment"] != null)
                {
                    model.RComment = row["RComment"].ToString();
                }
                if (row["RNamePY"] != null)
                {
                    model.RNamePY = row["RNamePY"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select AreaTypeID,Rtypeid,Parid,Leveal,Rsonnum,RsonCount,RuserCode,Rfullname,Rname,RComment,RNamePY ");
            strSql.Append(" FROM AreaType ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" AreaTypeID,Rtypeid,Parid,Leveal,Rsonnum,RsonCount,RuserCode,Rfullname,Rname,RComment,RNamePY ");
            strSql.Append(" FROM AreaType ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM AreaType ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.AreaTypeID desc");
            }
            strSql.Append(")AS Row, T.*  from AreaType T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表 扩展
        /// </summary>
        public DataSet GetListForUIExpand(string strWhere)
        {
            var strSql = @"
select c.AreaTypeID,c.Rtypeid,c.Parid,c.Leveal,c.Rsonnum,c.RsonCount,c.RuserCode,c.Rfullname,c.Rname,c.RComment,c.RNamePY,
	isnull(p.AreaTypeID,0) Parent_AreaTypeID,p.RuserCode Parent_RuserCode,p.Rname Parent_Rname
from areatype c
left join AreaType p on p.Rtypeid=c.Parid";
            if (strWhere.Trim() != string.Empty)
            {
                strSql += " where " + strWhere;
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

